package com.dalei.vds.xml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.dalei.vds.model.ImageType;

public class ImageTypeXMLReader {
	
	private InputStream source = null;
	
	private Document document = null;
	
	public ImageTypeXMLReader(InputStream source) {
		// TODO Auto-generated constructor stub
		this.source = source;
	}
	
	public void read() throws DocumentException {
		SAXReader reader = new SAXReader();
		document = reader.read(source);
	}
	
	public List<ImageType> imageTypes() {
		if (document != null) {
			Element root = document.getRootElement();
			List<ImageType> imageTypes = new ArrayList<ImageType>();
			for (Iterator<?> iterator = root.elementIterator(); iterator.hasNext(); ) {
				Element element = (Element) iterator.next();
				ImageType imageType = new ImageType();
				for (Iterator<?> i = element.attributeIterator(); i.hasNext(); ) {
					Attribute attr = (Attribute) i.next();
					String attributeName = attr.getName();
					String attributeValue = attr.getValue();
					
					if (attributeName.equals("id")) {
						int id = -1;
						try {
							id = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						imageType.setId(id);
					} else if (attributeName.equals("value")) {
						int value = -1;
						try {
							value = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						imageType.setValue(value);
					} else if (attributeName.equals("code")) {
						String code = attributeValue;
						imageType.setCode(code);
					}
				}
				String text = element.getText();
				imageType.setText(text);
				
				imageTypes.add(imageType);
			}
			return imageTypes;
		} else {
			return null;
		}
	}
	
	public ImageType getImageTypeById(int imageTypeId) {
		if (document != null) {
			Element root = document.getRootElement();
			Element element = getChildElementById(root, imageTypeId);
			if (element != null) {
				ImageType imageType = new ImageType();
				for (Iterator<?> i = element.attributeIterator(); i.hasNext(); ) {
					Attribute attr = (Attribute) i.next();
					String attributeName = attr.getName();
					String attributeValue = attr.getValue();
					
					if (attributeName.equals("id")) {
						int id = -1;
						try {
							id = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						imageType.setId(id);
					} else if (attributeName.equals("value")) {
						int value = -1;
						try {
							value = Integer.parseInt(attributeValue);
						} catch (NumberFormatException e) {
							// TODO: handle exception
							e.printStackTrace();
							return null;
						}
						imageType.setValue(value);
					} else if (attributeName.equals("code")) {
						String code = attributeValue;
						imageType.setCode(code);
					}
				}
				String text = element.getText();
				imageType.setText(text);
				
				return imageType;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	private Element getChildElementById(Element parent, int id) {
		if (parent != null) {
			for (Iterator<?> iterator = parent.elementIterator(); iterator
					.hasNext();) {
				Element element = (Element) iterator.next();
				for (Iterator<?> i = element.attributeIterator(); i.hasNext();) {
					Attribute attr = (Attribute) i.next();
					if (attr.getName().equals("id")
							&& attr.getValue().equals(Integer.toString(id))) {
						return element;
					}
				}
			}
			return null;
		} else {
			return null;
		}
	}
}
